#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2
import re
from pyquery import PyQuery as pq
import json
def getHtml(url, line_num, key):

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
        'Accept':'*/*',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'Accept-Encoding': 'gzip,deflate,sdch',
        'Connection': 'keep-alive',
        'Referer': 'http: // www.sse.com.cn / market / stockdata / overview / day /',

         }
    request = urllib2.Request(url, headers=headers)
    res=  urllib2.urlopen(request)
    #print res.getcode()
    the_page = res.read()
    #print the_page
    data= the_page.replace('jsonpCallback70306(',' ')
    data= data.replace('jsonpCallback359(',' ')
    data= data.replace(')',' ')
    ret=json.loads(data)
    #print(ret['result'][2]['trdAmt1'])
    #print(ret)
    res.close()
    if ret['result'][line_num][key] == "":
        return None
    else:
        return ret['result'][line_num][key]


if __name__ == "__main__":
    date = "2017-02-03"
    gp_line_num = 2
    gp_key = 'trdAmt1'
    jj_line_num = 1
    jj_key = 'trdAmt1'
    html_gp = "http://query.sse.com.cn/marketdata/tradedata/queryTradingByProdTypeData.do?jsonCallBack=jsonpCallback70306&searchDate="+date+"&prodType=gp&_=1485238356920"
    html_jj = "http://query.sse.com.cn/marketdata/tradedata/queryTradingByProdTypeData.do?jsonCallBack=jsonpCallback359&searchDate="+date+"&prodType=jj&_=1486348626560"
    data_gp = getHtml(html_gp, gp_line_num, gp_key)
    print(data_gp)
    data_jj = getHtml(html_jj, jj_line_num, jj_key)
    print(data_jj)
                